{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Building Molecular Hamiltonian\n",
    "*Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved.*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Overview\n",
    "\n",
    "In this tutorial, we will demonstrate how to use Paddle Quantum's `qchem` module to build valid Hamiltonian for simulating chemical molecules on a quantum computer. We will go step by step how to build the second quantized Hamiltonian from a molecular structure and how to transform it to a set of Pauli matrices. \n",
    "\n",
    "Hamiltonian is a physical quantity related to the total energy of a physical system. In general, it can be represented as \n",
    "\n",
    "$$\n",
    "\\hat{H}=\\hat{T}+\\hat{V},\\tag{1}\n",
    "$$\n",
    "\n",
    "where $\\hat{T}$ is the kinetic energy and $\\hat{V}$ is the potential energy. Hamiltonian is useful for various quantum algorithms, such as [variational quantum eigensolver](./VQE_EN.ipynb) and [Hamiltonian Simulation with Product Formula](./HamiltonianSimulation_EN.ipynb).\n",
    "\n",
    "When trying to solve a chemistry problem with quantum mechanics, we also need to write down a Hamiltonian that describes the chemical system involved in the problem. Starting from this Hamiltonian, we can, in principle, calculate the ground state and excited states, and use the information to further explore all the physical properties of the quantum system. The dominant Hamiltonian of electronic problems has the form\n",
    "\n",
    "$$\n",
    "\\hat{H}=\\sum_{i=1}^N\\left(-\\frac{1}{2}\\nabla_{x_i}^2\\right)+\\sum_{i=1}^N\\sum_{j< i}\\frac{1}{|x_i-x_j|}-\\sum_{i=1}^N\\sum_{I=1}^M\\frac{Z_I}{|x_i-R_I|},\\tag{2}\n",
    "$$\n",
    "\n",
    "when we use [atomic units](https://en.wikipedia.org/wiki/Hartree_atomic_units). Our electronic problem contains $N$ electrons and $M$ nucleus. We use $x_i$ to denote position of the $i$-th electron, and use $R_I$ to denote position of the $I$-th nuclei. \n",
    "\n",
    "This tutorial will have the following parts. Let's first talk about how to construct a molecule in `qchem`. After that, we will briefly describe how to calculate [Hartree Fock](https://en.wikipedia.org/wiki/Hartree%E2%80%93Fock_method) single particle orbitals by calling external quantum chemistry within Paddle Quantum. Next, we show how we can obtain the Hamiltonian in second quantization representation. Finally, we describe how to transform the Fermionic Hamiltonian to Pauli strings recognized by quantum computer."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Defining the molecular structure\n",
    "In this example, we show how to construct water molecule from its chemical formula and coordinates of atoms. \n",
    "\n",
    "![h2o.png](figures/buildingmolecule-fig-h2o.png)\n",
    "\n",
    "Within Paddle Quantum, we specify the atom as a list whose first element is the atomic symbol and the second element is another list that contains its Cartesian coordinate. The molecule is thus a bigger list composed of atoms' list.\n",
    "\n",
    "**Note: As to the environment setting, please refer to [README.md](https://github.com/PaddlePaddle/Quantum/blob/master/README.md).**"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "# Eliminate noisy python warnings\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "# in Angstrom\n",
    "h2o_structure_direct = [[\"H\", [-0.02111417,0.8350417,1.47688078]],  # H stands for hydrogen element in water\n",
    "                        [\"O\", [0.0, 0.0, 0.0]],                     # O stands for oxygen element in water\n",
    "                        [\"H\", [-0.00201087,0.45191737,-0.27300254]]]"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Instead of specifying molecular structure directly, we can also pass the \\*.xyz file to the `geometry` function to get the same structure."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "from paddle_quantum.qchem import geometry\n",
    "\n",
    "h2o_structure_xyz = geometry(file=\"h2o.xyz\")\n",
    "assert h2o_structure_xyz == h2o_structure_direct"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Calculate Hartree Fock orbitals\n",
    "Hartree Fock method uses the [Slater determinant](https://en.wikipedia.org/wiki/Slater_determinant) to represent the $N$-electron wavefunction. It could provide us with a set of single particle orbitals which are often taken as input to more advanced quantum chemistry methods. \n",
    "\n",
    "Paddle Quantum uses psi4 [1] as its quantum chemistry engine. We could use the `get_molecular_data` function provided in `qchem` module to manage the quantum chemistry calculation and get the necessary information about the molecule. `get_molecular_data` function takes molecular structure, total molecular charge, and spin multiplicity as its major inputs, it will return an OpenFermion [2] `MolecularData` object. \n",
    "\n",
    "Let's continue with our water molecule example. To run the Hartree Fock calculation, we need to set the `method` keyword argument to *scf* (Self Consistent Field). We can also improve the quality of Hartree Fock calculation by specifying the type of [basis set](https://en.wikipedia.org/wiki/Basis_set_(chemistry)) in the `basis` argument. "
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "from paddle_quantum.qchem import get_molecular_data\n",
    "\n",
    "h2o_moledata = get_molecular_data(\n",
    "    h2o_structure_direct,\n",
    "    charge=0,                # Water molecule is charge neutral\n",
    "    multiplicity=1,          # In the ground state, the lowest 5 molecular orbitals of water molecular will be occupied by a pair of electrons with opposite spin\n",
    "    basis=\"sto-3g\",\n",
    "    method=\"scf\",\n",
    "    if_save=True,            # Whether to save information contained in MolecularData object to a hdf5 file\n",
    "    if_print=True,           # Wheter to print the ground state energy of water molecule\n",
    "    name=\"\",                 # Specifies the name of the hdf5 file\n",
    "    file_path=\".\"            # Specifies where to store the hdf5 file          \n",
    ")\n",
    "\n",
    "from openfermion.chem import MolecularData\n",
    "\n",
    "assert isinstance(h2o_moledata, MolecularData)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Hartree-Fock energy for H2-O1_sto-3g_singlet (10 electrons) is -73.96770387867429.\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Molecular Hamiltonian in second quantization form\n",
    "When we study many electron quantum systems, it's often convenient to write Hamiltonian at the beginning of this tutorial in [second quantization](https://en.wikipedia.org/wiki/Second_quantization) representation \n",
    "\n",
    "$$\n",
    "\\hat{H}=\\sum_{p,q}h_{pq}\\hat{c}^{\\dagger}_p\\hat{c}_q+\\frac{1}{2}\\sum_{p,q,r,s}v_{pqrs}\\hat{c}^{\\dagger}_p\\hat{c}^{\\dagger}_q\\hat{c}_r\\hat{c}_s,\\tag{3}$$\n",
    "\n",
    "where $p$, $q$, $r$ and $s$ are Hartree Fock orbitals computed in the previous section. $\\hat{c}^{\\dagger}_p$ and $\\hat{c}_q$ are creation and annihilation operations, respectively. The two coefficients $h_{pq}$ and $v_{pqrs}$ are called molecular integrals, and can be obtained from `MolecularData` object in the following way."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "import numpy as np \n",
    "np.set_printoptions(precision=4, linewidth=150)\n",
    "\n",
    "hpq, vpqrs = h2o_moledata.get_integrals()\n",
    "assert np.shape(hpq)==(7, 7)             # When use sto3g basis, the total number of molecular orbitals used in water calculation is 7\n",
    "assert np.shape(vpqrs)==(7, 7, 7, 7)\n",
    "\n",
    "print(hpq)\n",
    "# print(vpqrs)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[[-3.2911e+01  5.5623e-01  2.8755e-01  9.7627e-16 -7.4568e-02 -9.4552e-02  2.8670e-01]\n",
      " [ 5.5623e-01 -8.0729e+00 -4.0904e-02  4.2578e-16  1.7890e-01  3.5048e-01 -1.3460e+00]\n",
      " [ 2.8755e-01 -4.0904e-02 -7.3355e+00  1.1465e-16  4.1911e-01  5.2109e-01  7.0928e-01]\n",
      " [ 9.7627e-16  4.2578e-16  1.1465e-16 -7.5108e+00  4.1730e-15  8.3317e-15 -8.4993e-16]\n",
      " [-7.4568e-02  1.7890e-01  4.1911e-01  4.1730e-15 -5.7849e+00  2.0887e+00  1.2427e-01]\n",
      " [-9.4552e-02  3.5048e-01  5.2109e-01  8.3317e-15  2.0887e+00 -5.0803e+00  1.3967e-02]\n",
      " [ 2.8670e-01 -1.3460e+00  7.0928e-01 -8.4993e-16  1.2427e-01  1.3967e-02 -5.0218e+00]]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Most of the time, we don't need to extract those integrals and assemble the Hamiltonian manually, *qchem* module has already helped us take care of this by providing the `fermionic_hamiltonian` function."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "from paddle_quantum.qchem import fermionic_hamiltonian\n",
    "\n",
    "H_of_water = fermionic_hamiltonian(\n",
    "    h2o_moledata,\n",
    "    multiplicity=1,\n",
    "    active_electrons=4,\n",
    "    active_orbitals=4\n",
    ")\n",
    "\n",
    "from openfermion.ops import FermionOperator\n",
    "\n",
    "assert isinstance(H_of_water, FermionOperator)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "By specifying `active_electrons` and `active_orbitals` keyword arguments, we can reduce the number of freedom of our Hamiltonian and thus reduce the number of terms in the spin Hamiltonian described in the next section. We can also use `active_space` function in *qchem* to return a list of *core* orbitals and *active* orbitals. "
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "from paddle_quantum.qchem import active_space\n",
    "\n",
    "core_orbits_list, act_orbits_list = active_space(\n",
    "    10,                        # number of electrons in water molecule\n",
    "    7,                         # number of molecular orbitals in water molecule\n",
    "    active_electrons=4,\n",
    "    active_orbitals=4\n",
    ")\n",
    "\n",
    "print(\"List of core orbitals: {:}\".format(core_orbits_list))\n",
    "print(\"List of active orbitals: {:}\".format(act_orbits_list))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "List of core orbitals: [0, 1, 2]\n",
      "List of active orbitals: [3, 4, 5, 6]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## From Fermionic Hamiltonian to spin Hamiltonian\n",
    "In quantum computing, we only have qubit operators composed of Pauli matrices\n",
    "\n",
    "$$\n",
    "\\boldsymbol{\\sigma}_x=\\begin{pmatrix}\n",
    "0 & 1\\\\\n",
    "1 & 0\n",
    "\\end{pmatrix},\\quad \\boldsymbol{\\sigma}_y=\\begin{pmatrix}\n",
    "0 & -i\\\\\n",
    "i & 0\n",
    "\\end{pmatrix},\\quad \\boldsymbol{\\sigma}_z=\\begin{pmatrix}\n",
    "1 & 0\\\\\n",
    "0 & -1\n",
    "\\end{pmatrix}.\\tag{4}\n",
    "$$\n",
    "\n",
    "Therefore, we need to transform our Hamiltonian in the previous section to qubit operators, [Jordan-Wigner transform](https://en.wikipedia.org/wiki/Jordan%E2%80%93Wigner_transformation) is one of the well-known methods to realize the transformation.\n",
    "> Alternatively, we also provide Bravyi-Kitaev transformation, by changing the argument, mapping_method, to 'bravyi_kitaev'.\n",
    "\n",
    "In *paddle quantum*, Hamiltonian is encoded in *pauli_str*. To avoid tedious manipulation of *string* object, we have provided `spin_hamiltonian` function which can generate the needed *pauli_str* from molecular structure on the fly."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "from paddle_quantum.qchem import spin_hamiltonian\n",
    "\n",
    "pauli_H_of_water_ = spin_hamiltonian(\n",
    "    h2o_moledata,\n",
    "    multiplicity=1,\n",
    "    active_electrons=4,\n",
    "    active_orbitals=4,\n",
    "    mapping_method='jordan_wigner'\n",
    ")\n",
    "\n",
    "print('There are ', pauli_H_of_water_.n_terms, 'terms in H2O Hamiltonian in total.')\n",
    "print('The first 10 terms are \\n', pauli_H_of_water_[:10])"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "There are  193 terms in H2O Hamiltonian in total.\n",
      "The first 10 terms are \n",
      " -72.10615980544183 I\n",
      "-0.007310917992546845 X0, X1, Y2, Y3\n",
      "0.005246087073083395 X0, X1, Y2, Z3, Z4, Y5\n",
      "0.0016283548447088131 X0, X1, Y2, Z3, Z4, Z5, Z6, Y7\n",
      "0.005246087073083395 X0, X1, X3, X4\n",
      "0.0016283548447088131 X0, X1, X3, Z4, Z5, X6\n",
      "-0.005994544380559041 X0, X1, Y4, Y5\n",
      "0.0013876441781026563 X0, X1, Y4, Z5, Z6, Y7\n",
      "0.001387644178102656 X0, X1, X5, X6\n",
      "-0.009538223793221256 X0, X1, Y6, Y7\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Great! Now you know how to build a proper Hamiltonian from a given molecular structure, let's move further and see how to use [variational quantum eigensolver](./VQE_EN.ipynb) (VQE) to determine the ground state of hydrogen molecule.\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "---\n",
    "## References\n",
    "\n",
    "[1] [Psi4: an open-source ab initio electronic structure program](https://wires.onlinelibrary.wiley.com/doi/abs/10.1002/wcms.93)\n",
    "\n",
    "[2] [OpenFermion: the electronic structure package for quantum computers\n",
    "](https://iopscience.iop.org/article/10.1088/2058-9565/ab8ebc)"
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "33c5d9dde9504794a92749aa5ec8a05187e008f4d7e74daf7ce0e98d5c6e004a"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}